package com.pap.car.info.agent.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.string.StringUtilss;
import com.pap.car.agent.ICarAgent;
import com.pap.car.agent.dto.CarInfoTreeDTO;
import com.pap.car.info.auto.entity.Car;
import com.pap.car.info.auto.mapper.CarMapper;
import com.pap.obj.vo.response.ResponseVO;

@Transactional
@Service("carAgent")
public class CarAgentImpl extends PapBaseServiceImpl<Car> implements ICarAgent {

	@Autowired
	private CarMapper carMapper;

	@Override
	public ResponseVO<Car> updateAndCheckCar(Car inputCar, String loginUserId) {
		Car tempForPath = mapper.selectByPrimaryKey(inputCar.getId());
		Car tempParentForPath = mapper.selectByPrimaryKey(inputCar.getParentId());
		if(tempParentForPath != null) {
			if (StringUtilss.isNotEmpty(tempParentForPath.getPathIds())) {
				tempForPath.setPathIds(tempParentForPath.getPathIds() + "," + tempParentForPath.getId());
			} else {
				tempForPath.setPathIds(tempParentForPath.getId());
			}
			if (StringUtilss.isNotEmpty(tempParentForPath.getPathCodes())) {
				tempForPath.setPathCodes(tempParentForPath.getPathCodes() + "," + tempParentForPath.getCode());
			} else {
				tempForPath.setPathCodes(tempParentForPath.getCode());
			}
			if (StringUtilss.isNotEmpty(tempParentForPath.getPathNames())) {
				tempForPath.setPathNames(tempParentForPath.getPathNames() + "," + tempParentForPath.getName());
			} else {
				tempForPath.setPathNames(tempParentForPath.getName());
			}
		}
		tempForPath.setParentId(inputCar.getParentId());
		tempForPath.setCode(inputCar.getCode());
		tempForPath.setName(inputCar.getName());
		tempForPath.setOutColor(inputCar.getOutColor());
		tempForPath.setInColor(inputCar.getInColor());
		tempForPath.setOutVolumn(inputCar.getOutVolumn());
		tempForPath.setSeatsNum(inputCar.getSeatsNum());
		tempForPath.setModifyUser(loginUserId);
		tempForPath.setModifyIp("0.0.0.0");
		tempForPath.setModifyTime(DateUtils.getCurrDateTimeStr());
		mapper.updateByPrimaryKey(tempForPath);
		return null;
	}

	@Override
	public List<CarInfoTreeDTO> selectCarInfoTreeJSON(String clientLicenseId, String bigTypeFlag) {
		Map<Object, Object> childrenMap = new HashMap<Object, Object>();
		childrenMap.put("myLesser_typeFlag", bigTypeFlag);
		childrenMap.put("clientLicenseId", clientLicenseId);

		List<CarInfoTreeDTO> childTreeNodes = carMapper.selectTreeNode(childrenMap);

		return buildByRecursive(childTreeNodes);
	}

	/**
	 * 使用递归方法建树 此功能把所有节点都查询出来，之后转换为树形结构
	 * 
	 * @param treeNodes
	 * @return
	 */
	public static List<CarInfoTreeDTO> buildByRecursive(List<CarInfoTreeDTO> treeNodes) {
		List<CarInfoTreeDTO> trees = new ArrayList<CarInfoTreeDTO>();
		for (CarInfoTreeDTO treeNode : treeNodes) {
			if (StringUtilss.isEmpty(treeNode.getParentId())) {
				trees.add(findChildren(treeNode, treeNodes));
			}
		}
		return trees;
	}

	/**
	 * 递归查找子节点
	 * 
	 * @param treeNodes
	 * @return
	 */
	public static CarInfoTreeDTO findChildren(CarInfoTreeDTO treeNode, List<CarInfoTreeDTO> treeNodes) {
		for (CarInfoTreeDTO it : treeNodes) {
			if (treeNode.getId().equals(it.getParentId())) {
				if (treeNode.getChildren() == null) {
					treeNode.setChildren(new ArrayList<CarInfoTreeDTO>());
				}
				treeNode.getChildren().add(findChildren(it, treeNodes));
			}
		}
		return treeNode;
	}
}
